import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;


public class TableTester {

    public static void main(String[] args) throws UnknownHostException {

        RoutingTable table = new RoutingTable();

        String netID = "";
        int subnetMask = 0;
        String gateway = "";

        InetAddress id1 = InetAddress.getByName("254.128.0.0");
        netID = id1.toString();
        subnetMask = 16;
        gateway = "254.128.2.2";
        table.add(netID, subnetMask, gateway);

        InetAddress id2 = InetAddress.getByName("127.10.0.1");
        netID = id2.toString();
        subnetMask = 9;
        gateway = "/127.0.0.0";
        table.add(netID, subnetMask, gateway);

        table.print();

        String destination = "254.128.128.128";
        System.out.println("[in]  " + destination);
        System.out.println("[out] " + table.getEndpoint(destination));
        System.out.println();

        destination = "127.10.0.1";
        System.out.println("[in]  " + destination);
        System.out.println("[out] " + table.getEndpoint(destination));
        System.out.println();

        destination = "127.10.0.0";
        System.out.println("[in]  " + destination);
        System.out.println("[out] " + table.getEndpoint(destination));
        System.out.println();

        table.delete("127.10.0.1");

        table.print();

        table.delete("127.10.0.1");

        table.print();



        //        int mask = 0;
        //        
        //        if (subnet > 0) {
        //            mask = 0x80000000;
        //            mask = mask >> (subnet - 1);
        //        }
        //        
        //        System.out.println(mask);
        //        
        //        String s = "128";
        //        
        //        int value = 255;
        //        
        //        byte sv = Integer.valueOf(s).byteValue();
        //        
        //        System.out.println(sv);
        //        System.out.println(value);
        //        
        //        int value2 = value & sv;
        //        
        //        System.out.println(value2);
        // 

        //        String ip = "0.255.173.255";
        //        
        //        String[] sliced = ip.split("\\.");
        //        
        //        StringBuilder result = new StringBuilder();
        //        
        //        int b = 0;
        //        byte a = 0;
        //        
        //        for (int i = 0; i < sliced.length - 1; i++) {
        //            a = Integer.valueOf(sliced[i]).byteValue();
        //            b = Integer.valueOf(sliced[i]);
        //                                           
        //            result.append(a & b);
        //            result.append(".");
        //        }
        //        
        //        a = Integer.valueOf(sliced[3]).byteValue();
        //        b = Integer.valueOf(sliced[3]);
        //        
        //        result.append(a & b);
        //        
        //        System.out.println(result);
        //        
        //        
        //        // Calculate the subnet mask.
        //        subnet = 16;
        //              
        //        mask = 0;
        //        
        //        if (subnet > 0) {
        //            mask = 0x80000000;
        //            mask = mask >> (subnet - 1);
        //        }
        //        
        //        // Calculate the IP using the subnet mask.
        //        int octet1Mask = 0x00FFFFFF;
        //        int octet2Mask = 0xFF00FFFF;
        //        int octet3Mask = 0xFFFF00FF;
        //        int octet4Mask = 0xFFFFFF00;
        //
        //        String ip = "254.128.128.128";
        //
        //        // Isolate each octet from the ip.
        //        String[] sliced = ip.split("\\.");
        //
        //        // Assemble an integer ip value from the octets.
        //        int octet1 = Integer.valueOf(sliced[0]) << 24;
        //        int octet2 = Integer.valueOf(sliced[1]) << 16;
        //        int octet3 = Integer.valueOf(sliced[2]) << 8;
        //        int octet4 = Integer.valueOf(sliced[3]);
        //
        //        int ipValue = octet1 + octet2 + octet3 + octet4;
        //          
        //        // Perform the bitwise-and operation.
        //        System.out.println("[mask]     " + mask);
        //        System.out.println("[ipValue]  " + ipValue);
        //        System.out.println("[m & ipv]  " + (ipValue & mask));
        //        
        //        // Extract the calculated octets.
        //        System.out.println("[oct1]     " + (( (((ipValue & mask) | octet1Mask) ^ octet1Mask) >> 24)));
        //        System.out.println("[oct2]     " + ( (((ipValue & mask) | octet2Mask)  ^ octet2Mask) >> 16));
        //        System.out.println("[oct3]     " + ( (((ipValue & mask) | octet3Mask) ^ octet3Mask) >> 8));
        //        System.out.println("[oct4]     " + (((ipValue & mask) | octet4Mask) ^ octet4Mask));
        //        
        //        // Assemble the calculated ip.

    }
}
